package com.fz.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created by webrx on 2017-09-07.
 */
public class MyInterceptor implements HandlerInterceptor{
    public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object o) throws Exception {
        //预处理，请求处理前，false不让过，true通过
        System.out.println("preHandle(HttpServletRequest req, HttpServletResponse resp, Object o)");
        //所以需要在这里判断是否登录，让不让过
        HttpSession session=req.getSession();
        if(session.getAttribute("yname")==null){

        //重定向跳走
        resp.sendRedirect("/login.jsp");
        //转发跳走
       // req.getRequestDispatcher("/login.jsp").forward(req,resp);
        }
        return true;
    }

    public void postHandle(HttpServletRequest req, HttpServletResponse resp, Object o, ModelAndView mv) throws Exception {
        System.out.println(req.getAttribute("user"));
        System.out.println("postHandle(HttpServletRequest req, HttpServletResponse resp, Object o, ModelAndView mv)");
    }

    public void afterCompletion(HttpServletRequest req, HttpServletResponse resp, Object o, Exception e) throws Exception {
        System.out.println(req.getAttribute("user"));
        System.out.println("afterCompletion(HttpServletRequest req, HttpServletResponse resp, Object o, Exception e)");
    }
}
